<!doctype html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/style.css" />
		<link rel="stylesheet" href="../css/iconfont.css " />
		<link href="../css/calendar/calendar.css" rel="stylesheet"/>
		<style>
			.file{position: absolute;top: 1rem;width: 2rem;height: 3rem;left: calc(50% - 1rem);z-index: 10000;opacity: 0;}
		</style>
	</head> 
	<body>
		<header class="mui-bar mui-bar-nav ding-bar">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">更改头像</h1>
		</header>
		<div class="mui-content">
		    <div id="gerenzhongxin">
		        <div class="gl-gr-banner" style="position: relative;">
		        	<div class="gl-tx" id="avatar">
		        		<div ></div>
		        		<img src="../images/zhuce-touxiang.png"/>
		        	</div>
		        	<h1 class="gl-gr-name">更改头像</h1>
		        	<!--<input  type="file" accept="image/*" capture="camera" class="file">-->
		        </div>
		    </div>
		    <button type="button" id="shiyong" style="display: block;width: 50%;margin: 80px auto;color: #fff;background: #ff7e00;border: none;">确认使用</button>
		</div>
		<script src="../js/jquery-1.8.0.min.js"></script>
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/head_public.js" ></script>
		<script type="text/javascript" src="../js/common.js" ></script>
		<script type="text/javascript" src="../js/session.js" ></script>
		<script type="text/javascript" src="../js/uploadPreview.js" ></script>
		<script type="text/javascript">
            $("footer").load("footer3.html")
            $(".mui-content").on("tap","a",function(){
				mui.openWindow({url:$(this).attr("data-href")})
			})
            var userid=getCookie("userid")
            if(userid==""||userid==null){
            	alert("您还没有登录，请先登录！")
            	location.href="login.html";
            }else{
            	var data1={};
            	data1.member_id=userid;	
            	$.post(net+"public/index.php?s=api/member/member_avatar",data1,function(res){
            		if(res.code=="200"){
            			if(res.result.member_avatar!=null&&res.result.member_avatar!=""){
            				$("#avatar").html('<div ></div><img src="'+net+res.result.member_avatar+'" />')
            			}
            		}
            	})
            	
            	//图片预览
//          	$(".file").uploadPreview({ Img: "avatar", Width: 120, Height: 120 });
            	mui(".mui-content").on("tap","#shiyong",function(){
            		upload(userid)
//          		var formdate= new FormData();
//          		formdate.append("touxiang",$(".file")[0].files[0]);
//          		formdate.append("member_id",userid);
//          		$.ajax({
//          			type:"post",
//          			url:net+"public/index.php?s=api/member/avatar_uplode",
//          			async:true,
//          			data:formdate,
//          			processData:false,
//          			success:function(res){
//          				
//          			}
            		});
//          		$.post(net+"public/index.php?s=api/member/avatar_uplode",formdate,function(res){
//          			
//          		})
//          	})
            }
//          var files = document.getElementById('avatar');
     
		</script>
		<script>
            var imgpath="";
			 //扩展API完成后执行的操作
                function plusReady(){               
                 //给选中的li增加判别class
                     mui(".mui-content").on("tap",".gl-tx",function(){
                     	   page.imgUp();
                     })  
                } 
                //弹出系统按钮选择框
                var page=null; 
                page={ 
                    imgUp:function(){ 
                        var m=this; 
                       /* console.log(m);*/
                        plus.nativeUI.actionSheet({cancel:"取消",buttons:[ 
                            {title:"拍照"}, 
                            {title:"从相册中选择"} 
                        ]}, function(e){//1 是拍照  2 从相册中选择 
                            switch(e.index){ 
                                case 1:appendByCamera();break; 
                                case 2:appendByGallery();break; 
                            } 
                        }); 
                    } 
                } 
               
            // 拍照添加文件
            function appendByCamera(){
                plus.camera.getCamera().captureImage(function(e){
                    console.log("e is" +  e);
                    plus.io.resolveLocalFileSystemURL(e, function(entry) {
                        var path = entry.toLocalURL();
                       $("#avatar").html('<div></div><img id="tx" src="'+path+'"/>')
                       imgpath = path;
                    }, function(e) {
                        mui.toast("读取拍照文件错误：" + e.message);
                    });      
                });   
            }
            // 从相册添加文件
            function appendByGallery(){
                plus.gallery.pick(function(path){
                    imgpath=path;
                    $("#avatar").html('<div></div><img id="tx" src="'+path+'"/>')
                });
            }
             
               
            //服务端接口路径
            var server = net+"public/index.php?s=api/member/avatar_uplode";
            //获取图片元素
            var files = $('#avatar #tx');
            // 上传文件
            function upload(userid){
                var wt=plus.nativeUI.showWaiting();
                var task=plus.uploader.createUpload(server,
                    {method:"POST"
                   
                    },
                    function(t,status){ //上传完成
                    	
                        if(status==200){
                            
                            wt.close(); //关闭等待提示按钮
//                          location.reload();
                            mui.back();
//                          location.href="../public/myself.html"
                        }else{
                            alert("上传失败："+status);
                            wt.close();//关闭等待提示按钮
                        }
                    }
                );  
                
                //添加其他参数
                task.addData("member_id",userid);
                task.addFile(imgpath,{key:"member_avatar"});
                task.start();
            } 
          //扩展API是否准备好，如果没有准备好则监听plusReady 
         if(window.plus){
                plusReady();
            }else{
                document.addEventListener("plusready",plusReady,false);
            }   
		</script>
	</body>

</html>